package service;

import com.mysql.cj.log.Log;
import model.*;

import java.util.ArrayList;

public class ServiceTodo {

    /**
     * 判断todoId和当前userId是否一样
     *
     * @param todoId
     * @param currentUserId
     * @return
     */
    public static boolean currentUserTodo(int todoId, int currentUserId) {
        // 判断当前是否是本用户登录
        String sql = "SELECT * FROM `blog`.`todo` WHERE id = ? AND userId = ?";
        ArrayList<Todo> todos = MySQLStorage.select(
                sql,
                resultSet -> {
                    int id = resultSet.getInt("id");
                    String content = resultSet.getString("content");
                    int userId = resultSet.getInt("userId");
                    return new Todo(id, content, userId);
                },
                statement -> {
                    statement.setInt(1, todoId);
                    statement.setInt(2, currentUserId);
                }
        );
        boolean result = todos.size() > 0;
        return result;
    }

    /**
     * 显示当前用户的数据
     *
     * @param currentUser
     * @return
     */
    // 换web框架之后 service/服务这一块不用动
    public static ArrayList<Todo> currentUserTodos(User currentUser) {
        String sql = "SELECT * FROM `blog`.`todo` WHERE userId = ?";
        ArrayList<Todo> todos = MySQLStorage.select(
                sql,
                resultSet -> {
                    int id = resultSet.getInt("id");
                    String content = resultSet.getString("content");
                    int userId = resultSet.getInt("userId");
                    return new Todo(id, content, userId);
                },
                statement -> {
                    statement.setInt(1, currentUser.id);
                }
        );
        return todos;
    }

    public static void add(String content, User currentUser) {
        String sql = "INSERT INTO `blog`.`todo` (content, userId) VALUES (?, ?)";
        MySQLStorage.update(sql, statement -> {
            statement.setString(1, content);
            statement.setInt(2, currentUser.id);
        });
    }

    public static void delete(int id) {
        String sql = "DELETE FROM `blog`.`todo` WHERE id = ?";
        MySQLStorage.update(sql, statement -> {
            statement.setInt(1, id);
        });
    }

    public static void update(int id, String content) {
        String sql = "UPDATE `blog`.`todo` SET content = ? WHERE id = ?";
        MySQLStorage.update(sql, statement -> {
            statement.setString(1, content);
            statement.setInt(2, id);
        });
    }

    public static Todo todoNow(int todoId) {
        String sql = "SELECT * FROM `blog`.`todo` WHERE id = ?";
        Todo todo = MySQLStorage.selectOne(
                sql,
                resultSet -> {
                    int id = resultSet.getInt("id");
                    String content = resultSet.getString("content");
                    int userId = resultSet.getInt("userId");
                    return new Todo(id, content, userId);
                },
                statement -> {
                    statement.setInt(1, todoId);
                }
        );
        System.out.println("todo: " + todo);
        return todo;
    }

    public static ArrayList<Todo> find(String str, int currentId) {
        String sql = "SELECT * FROM `blog`.`todo` WHERE content LIKE ? AND userId = ?";
        ArrayList<Todo> todo = MySQLStorage.select(
                sql,
                resultSet -> {
                    int id = resultSet.getInt("id");
                    String content = resultSet.getString("content");
                    int userId = resultSet.getInt("userId");
                    return new Todo(id, content, userId);
                },
                statement -> {
                    statement.setString(1, "%" + str + "%");
                    statement.setInt(2, currentId);
                }
        );
        System.out.println("todo: " + todo);
        return todo;
    }
}
